package com.subaiqiao.yunshiyuan.entrance.ysy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.subaiqiao.yunshiyuan.basis.utils.Result;
import com.subaiqiao.yunshiyuan.entrance.ysy.pojo.SysConfig;
import com.subaiqiao.yunshiyuan.entrance.ysy.service.SysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 曹朝禹
 * @since 2022-05-13
 */
@RestController
@RequestMapping("/ysy/sys-config")
@Api(tags = "系统配置")
public class SysConfigController {

    @Autowired
    private SysConfigService sysConfigService;

    @ApiOperation("分页查询系统配置信息")
    @GetMapping("/FindSysConfigPage.do")
    public Result FindSysConfigPage(
            @RequestParam @ApiParam("页码") Integer pageNum,
            @RequestParam @ApiParam("数量") Integer pageSize,
            @RequestParam(defaultValue = "") @ApiParam("配置名称") String sysConfigName){
        QueryWrapper<SysConfig> wrapper = new QueryWrapper<>();
        wrapper.like("sys_config_name",sysConfigName);
        IPage<SysConfig> page = new Page<>(pageNum, pageSize);
        return Result.success(sysConfigService.page(page, wrapper));
    }

    @ApiOperation("查询系统配置信息")
    @GetMapping("/FindSysConfig.do")
    public Result FindSysConfig(
            @RequestParam(defaultValue = "") @ApiParam("配置名称") String sysConfigName){
        QueryWrapper<SysConfig> wrapper = new QueryWrapper<>();
        wrapper.like("sys_config_name",sysConfigName);
        return Result.success(sysConfigService.list(wrapper));
    }

    @ApiOperation("查询系统配置项详情信息")
    @GetMapping("/FindSysConfigDetails.do")
    public Result FindSysConfigDetails(
            @RequestParam(defaultValue = "") @ApiParam("配置编号") String sysConfigId,
            @RequestParam(defaultValue = "") @ApiParam("配置名称") String sysConfigName){
        QueryWrapper<SysConfig> wrapper = new QueryWrapper<>();
        if(!sysConfigId.equals("")) wrapper.eq("sys_config_Id",sysConfigId);
        wrapper.like("sys_config_name",sysConfigName);
        return Result.success(sysConfigService.list(wrapper));
    }

    @ApiOperation("添加系统配置项")
    @PostMapping("/InsertSysConfig.do")
    public Result InsertSysConfig(String sysConfigName,String sysConfigContent){
        SysConfig sysConfig = new SysConfig();
        sysConfig.setSysConfigName(sysConfigName);
        sysConfig.setSysConfigContent(sysConfigContent);
        sysConfig.setSysConfigStatus(0);
        return Result.success(sysConfigService.save(sysConfig));
    }

    @ApiOperation("修改系统配置项")
    @PutMapping("/UpdateSysConfig.do")
    public Result UpdateSysConfig(Integer sysConfigId, String sysConfigName,String sysConfigContent){
        SysConfig sysConfig = sysConfigService.getById(sysConfigId);
        sysConfig.setSysConfigName(sysConfigName);
        sysConfig.setSysConfigContent(sysConfigContent);
        return Result.success(sysConfigService.updateById(sysConfig));
    }

    @ApiOperation("删除系统配置项")
    @DeleteMapping("/DeleteSysConfig.do")
    public Result DeleteSysConfig(Integer sysConfigId){
        SysConfig sysConfig = sysConfigService.getById(sysConfigId);
        sysConfig.setSysConfigStatus(1);
        return Result.success(sysConfigService.updateById(sysConfig));
    }


    @ApiOperation("查询所有配置数量")
    @GetMapping("/FindSysConfigCount.do")
    public Result FindSysCategoryCount(){
        return Result.success(sysConfigService.FindSysConfigCount());
    }

    @ApiOperation("查询所有正常配置数量")
    @GetMapping("/FindSysConfigNormalCount.do")
    public Result FindSysConfigNormalCount(){
        return Result.success(sysConfigService.FindSysConfigNormalCount());
    }
}
